/**
 * Created by WeSexyAndWeKnowIt .Inc 
 * Member: JackIsSoBig
 * Date: Mar, 30 2012
 * Last update: April, 5 2012
 */

package gameGUI;

import gameLogic.GameController;
import gameLogic.Move;
import gameLogic.PlayerType;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.SwingWorker;

@SuppressWarnings("serial")
public class GameMainMenu extends JFrame {
	private SoundPlayer player; // Plays theme sound....

	private JButton bt1Player;
	private JButton bt2Player;
	private JButton btRandomAI;
	private JButton btBlockAI;
	private JButton btSmartAI;
	private JButton btMasterAI;
	private JButton btGoBack;
	private JButton btQuit;
	private ImageIcon imageBackground;

	private PlayerType player1Type;
	private PlayerType player2Type;
	private boolean player1AIDED;
	private boolean player2AIDED;
	private JLabel lbSpritePikachu;
	private JLabel lbSpriteDigimon;
	private BackgroundPanel pnBackground;
	private SpringLayout layout;
	private JCheckBox chkbCompAidedP1; // check box forComputer-Aided Human
										// Player 1
	private JCheckBox chkbCompAidedP2; // check box forComputer-Aided Human
										// Player 2

	private Container c;
	private GameBoard gb;

	public GameMainMenu(String gameTitle) {
		super(gameTitle);

		player = new SoundPlayer("sound/theme.mp3"); // jack change mp3 file
														// here

		player1Type = player2Type = null;
		imageBackground = new ImageIcon("gui/background.jpg");

		c = getContentPane();
		gb = new GameBoard(gameTitle, this);

		bt1Player = new JButton("1 Player");
		bt2Player = new JButton("2 Player");
		lbSpritePikachu = new JLabel(new ImageIcon("gui/pikachu_run.gif"));
		lbSpriteDigimon = new JLabel(new ImageIcon("gui/digimon_run.gif"));

		btRandomAI = new JButton("Random AI");
		btBlockAI = new JButton("Block AI");
		btSmartAI = new JButton("Smart AI");
		btMasterAI = new JButton("Master AI");
		btQuit = new JButton("    Quit    ");
		btGoBack = new JButton("Go Back");

		chkbCompAidedP1 = new JCheckBox("Computer-Aided Human Player 1");
		chkbCompAidedP2 = new JCheckBox("Computer-Aided Human Player 2");

		// create panel and layout
		pnBackground = new BackgroundPanel(imageBackground);
		layout = new SpringLayout();
		pnBackground.setLayout(layout);

		// add components
		pnBackground.add(bt1Player);
		pnBackground.add(bt2Player);
		pnBackground.add(chkbCompAidedP1);
		pnBackground.add(chkbCompAidedP2);
		pnBackground.add(btRandomAI);
		pnBackground.add(btBlockAI);
		pnBackground.add(btSmartAI);
		pnBackground.add(btMasterAI);
		pnBackground.add(lbSpritePikachu);
		pnBackground.add(lbSpriteDigimon);
		pnBackground.add(btGoBack);
		pnBackground.add(btQuit);

		layout.putConstraint(SpringLayout.WEST, bt1Player, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, bt1Player, 120,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, bt2Player, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, bt2Player, 150,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btQuit, 245, SpringLayout.WEST,
				pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btQuit, 180,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, chkbCompAidedP1, 175,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, chkbCompAidedP1, 220,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, chkbCompAidedP2, 175,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, chkbCompAidedP2, 250,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btRandomAI, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btRandomAI, 120,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btBlockAI, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btBlockAI, 150,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btSmartAI, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btSmartAI, 180,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btMasterAI, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btMasterAI, 90,
				SpringLayout.NORTH, pnBackground);

		layout.putConstraint(SpringLayout.WEST, btGoBack, 245,
				SpringLayout.WEST, pnBackground);
		layout.putConstraint(SpringLayout.NORTH, btGoBack, 300,
				SpringLayout.NORTH, pnBackground);

		c.add(pnBackground);

		lbSpritePikachu.setVisible(false);
		lbSpriteDigimon.setVisible(false);
		// temporary hide AI options
		btRandomAI.setVisible(false);
		btBlockAI.setVisible(false);
		btSmartAI.setVisible(false);
		btMasterAI.setVisible(false);
		btGoBack.setVisible(false);

		bt1Player.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				// show AI options and hide others buttons
				bt1Player.setVisible(false);
				bt2Player.setVisible(false);

				btRandomAI.setVisible(true);
				btBlockAI.setVisible(true);
				btSmartAI.setVisible(true);
				btMasterAI.setVisible(true);
				btGoBack.setVisible(true);
				chkbCompAidedP2.setEnabled(false);

			}
		});

		bt1Player.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpritePikachu.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpritePikachu, 325,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpritePikachu, 105,
						SpringLayout.NORTH, pnBackground);
			}
		});
		bt1Player.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpritePikachu.setVisible(false);
			}
		});
		bt2Player.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpritePikachu.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpritePikachu, 325,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpritePikachu, 135,
						SpringLayout.NORTH, pnBackground);
			}
		});
		bt2Player.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpritePikachu.setVisible(false);
			}
		});

		btQuit.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpritePikachu.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpritePikachu, 325,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpritePikachu, 165,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btQuit.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpritePikachu.setVisible(false);
			}
		});

		bt2Player.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				// tell GameControl to play with another person
				choosePlayer(0);
			}
		});

		btQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JOptionPane
						.showMessageDialog(
								null,
								"this but button is supposed to quit the game but...There is a troll spirit in everyone of us so try to figure out how to exit the game by yourself.",
								"Troll", 3, null);
			}
		});

		btRandomAI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				choosePlayer(1);
			}
		});
		btRandomAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpriteDigimon.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpriteDigimon, 185,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpriteDigimon, 105,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btRandomAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpriteDigimon.setVisible(false);
			}
		});

		btBlockAI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				choosePlayer(2);
			}
		});
		btBlockAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpriteDigimon.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpriteDigimon, 185,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpriteDigimon, 135,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btBlockAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpriteDigimon.setVisible(false);
			}
		});

		btSmartAI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				choosePlayer(3);
			}
		});
		btSmartAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpriteDigimon.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpriteDigimon, 185,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpriteDigimon, 165,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btSmartAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpriteDigimon.setVisible(false);
			}
		});

		btMasterAI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				choosePlayer(4);
			}
		});
		btMasterAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpriteDigimon.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpriteDigimon, 185,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpriteDigimon, 75,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btMasterAI.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpriteDigimon.setVisible(false);
			}
		});

		chkbCompAidedP1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				player1AIDED = (player1AIDED == true) ? false : true;
			}
		});
		chkbCompAidedP2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				player2AIDED = (player2AIDED == true) ? false : true;
			}
		});

		btGoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				// show AI options and hide others buttons
				bt1Player.setVisible(true);
				bt2Player.setVisible(true);

				btRandomAI.setVisible(false);
				btBlockAI.setVisible(false);
				btSmartAI.setVisible(false);
				btMasterAI.setVisible(false);
				btGoBack.setVisible(false);
				chkbCompAidedP2.setEnabled(true);
			}
		});

		btGoBack.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseEntered(MouseEvent e) {
				lbSpriteDigimon.setVisible(true);
				layout.putConstraint(SpringLayout.WEST, lbSpriteDigimon, 185,
						SpringLayout.WEST, pnBackground);
				layout.putConstraint(SpringLayout.NORTH, lbSpriteDigimon, 285,
						SpringLayout.NORTH, pnBackground);
			}
		});
		btGoBack.addMouseListener(new MouseAdapter() {
			// invoked when mouse enters the form
			public void mouseExited(MouseEvent e) {
				lbSpriteDigimon.setVisible(false);
			}
		});
		// this.setSize(450, 150);
		this.setSize(imageBackground.getIconWidth(),
				imageBackground.getIconHeight());

		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		playThemeMusic();
	}// END OF CONSTRUCTION

	class BackgroundPanel extends JPanel {
		private ImageIcon imageIcon;

		public BackgroundPanel(ImageIcon imageBackground) {
			this.imageIcon = imageBackground;

		}

		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getIconWidth(),
					imageIcon.getIconHeight(), this);
		}
	}

	void hideFrame() {
		this.setVisible(false);
	}

	// temporary AI type: 0: 2 players | 1:randomAI | 2:blockAI | 3:smartAI |
	// 4:masterAI
	void choosePlayer(final int playerType) {
		hideFrame();

		// reset main menu for the next game
		bt1Player.setVisible(true);
		bt2Player.setVisible(true);

		btRandomAI.setVisible(false);
		btBlockAI.setVisible(false);
		btSmartAI.setVisible(false);
		btMasterAI.setVisible(false);

		chkbCompAidedP2.setEnabled(true);

		player1Type = (player1AIDED == true) ? PlayerType.HUMAN_AIDED
				: PlayerType.HUMAN;
		switch (playerType) {
		case 0:
			player2Type = (player2AIDED == true) ? PlayerType.HUMAN_AIDED
					: PlayerType.HUMAN;
			break;
		case 1:
			player2Type = PlayerType.AI_RANDOM;
			break;
		case 2:
			player2Type = PlayerType.AI_BLOCKING;
			break;
		case 3:
			player2Type = PlayerType.AI_SMART;
			break;
		case 4:
			player2Type = PlayerType.AI_MASTER;
			break;
		}
		System.out.println(player1Type.toString() + " "
				+ player2Type.toString());

		class GameWorker extends SwingWorker<Object, Object> {
			@Override
			public Object doInBackground() {
				runGame();
				return null;
			}
		}

		(new GameWorker()).execute();
	}

	void runGame() {
		player.close();

		GameController gc = new GameController(player1Type, player2Type);
		gb.playBattleTheme();
		gc.addBoardUpdatedEventHandler(gb.bue);
		gc.addGetUserMoveEventHandler(gb.uce);
		gb.setVisible(true);
		int result = gc.startGame();
		if (result == 111) {
			gb.showWinMessage(player1Type, "Player 1");
		} else if (result == 222) {
			gb.showWinMessage(player2Type, "Player 2");
		} else {
			gb.showWinMessage(null, null);
		}
		gb.setVisible(false);
		gb.resetBoard();
		gb.prepareNewGame();
		playThemeMusic();
	}

	void playThemeMusic() {
		player.play();

	}
}
